package com.thesis.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;

/**
 * Created by Anvy at 2020/2/19.
 */
public class ToolsUtils {

    private static  final Logger logger= LoggerFactory.getLogger(ToolsUtils.class);

    /**
     * 将百分数转换为BigDecimal
     * @param str e.g:10%
     * @return 0.1
     */
    public static BigDecimal stringToBigDecimal(String str){
        if(str==null){
            logger.debug("str is null");
            return null;
        }
        if(str.equals("0")||str.equals("0%")){
            return new BigDecimal(0);
        }
        //左闭右开
        String substring = str.substring(0, str.length() - 1);
        BigDecimal bigDecimal=new BigDecimal(substring);
        BigDecimal divide = bigDecimal.divide(new BigDecimal(100));
        logger.debug(str+" 转换后的结果为："+divide.doubleValue());
        return divide;
    }
}
